package com.qingmi.farmer.data;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.luck.picture.lib.rxbus2.RxBus;
import com.qingmi.farmer.data.model.Address;
import com.qingmi.farmer.data.model.AliPayAccount;
import com.qingmi.farmer.data.model.Cooperative;
import com.qingmi.farmer.data.model.UserInfo;
import com.wjjun.base.data.preference.UserPreference;
import com.qingmi.farmer.event.UserDataChangeEvent;

/**
 * 用户数据
 * Created by 67535 on 2018/5/18.
 */
public class UserData {

    private static UserData instance;

    private UserInfo userInfo;
    private UserPreference userPreference;

    private int unReadMsgCount = 0;
    private int unReadLogisticsMsgCount = 0;

    private UserData() {

    }

    public static UserData getInstance() {
        if (instance == null) {
            synchronized (UserData.class) {
                if (instance == null) {
                    instance = new UserData();
                }
            }
        }
        return instance;
    }

    public UserData init(UserPreference userPreference) {
        this.userPreference = userPreference;
        return this;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
        save();
    }

    public void setToken(String token) {
        userPreference.setToken(token);
    }

    public void setCooperative(Cooperative cooperative) {
        if (userInfo == null) {
            return;
        }
        userInfo.setCooperative(cooperative);
        save();
    }

    public void setBalance(double balance){
        if( userInfo == null){
            return;
        }
        userInfo.setTotalMoney(balance);
        save();
    }

    private void save() {
        userPreference.setUserInfo(new Gson().toJson(userInfo));
    }


    public void setAliPayAccount(AliPayAccount aliPayAccount) {
        if (userInfo == null) {
            return;
        }
        userInfo.setAliPayAccount(aliPayAccount);
        save();
    }

    public void setHasPayPwd() {
        if (userInfo == null) {
            return;
        }
        userInfo.setHasPayPassword(true);
        save();
    }

    /**
     * 通知用户数据变化
     */
    public void notifyDataChange(Class fromClass) {
        RxBus.getDefault().post(new UserDataChangeEvent().setFromClass(fromClass));
    }

    public boolean isLogin() {
        if (userInfo == null) {
            return false;
        }
        return true;
    }

    public void logout() {
        setUnReadLogisticsMsgCount(0);
        setUnReadMsgCount(0);
        setUserInfo(null);
        setToken(null);
        notifyDataChange(UserData.class);
    }

    /**
     * 是否加入合作社
     */
    public boolean isJoinCooperative() {
        if (userInfo != null && userInfo.getCooperative() != null
                && !TextUtils.isEmpty(userInfo.getCooperative().getCooperativeId())
                && (userInfo.getCooperative().getStatus() == DataSetting.COOPERATIVE_SIGNED_STATUS_SIGNED
                || userInfo.getCooperative().getStatus() == DataSetting.COOPERATIVE_SIGNED_STATUS_RESCIND_CHECK
                || (userInfo.getCooperative().getSigning() != null
                    && DataSetting.cooperativeIsSigned(userInfo.getCooperative().getSigning().getStatus()))  )) {
            return true;
        }

        return false;
    }

    /**
     * 是否绑定支付宝
     */
    public boolean hasAliPayAccount() {
        if (userInfo != null && userInfo.getAliPayAccount() != null
                && !TextUtils.isEmpty(userInfo.getAliPayAccount().getAliPayAccount())) {
            return true;
        }

        return false;
    }

    /**
     * 是否设置了支付密码
     */
    public boolean hasPayPwd() {
        if (userInfo != null && userInfo.getHasPayPassword()) {
            return true;
        }

        return false;
    }

    /**
     * 设置默认地址
     */
    public void setDefAddress(Address address) {
        if (userInfo == null) {
            return;
        }
        userInfo.setDefaultAddress(address);
        save();
    }

    public int getTotalUnReadMsgCount() {
        return unReadMsgCount + unReadLogisticsMsgCount;
    }

    public int getUnReadMsgCount() {
        return unReadMsgCount;
    }

    public void setUnReadMsgCount(int unReadMsgCount) {
        this.unReadMsgCount = unReadMsgCount;
    }

    public void readOneMessage() {
        if (unReadMsgCount > 0) {
            unReadMsgCount--;
        }
    }

    public int getUnReadLogisticsMsgCount() {
        return unReadLogisticsMsgCount;
    }

    public void setUnReadLogisticsMsgCount(int unReadLogisticsMsgCount) {
        this.unReadLogisticsMsgCount = unReadLogisticsMsgCount;
    }

    public void readOneLogisticsMsg() {
        if (unReadMsgCount > 0) {
            unReadMsgCount--;
        }
    }

    public String getAvatar() {
        return userInfo.getAvatar();
    }

    public String getNickName() {
        return userInfo.getNickName();
    }

    public String getUserName() {
        return userInfo.getUserName();
    }


    public String getUserType() {
        return userInfo.getUserType();
    }


    public Cooperative getCooperative() {
        if (userInfo == null) {
            return null;
        }
        return userInfo.getCooperative();
    }

    public String getSigningStatus() {
        return userInfo.getSigningStatus();
    }

    public String getPhone() {
        return userInfo.getPhone();
    }

    public int getGender() {
        return userInfo.getGender();
    }

    public String getCid() {
        return userInfo.getCid();
    }


    public String getAddress() {
        return userInfo.getAddress();
    }

    public String getCityId() {
        return userInfo.getCityId();
    }

    public Address getDefAddress() {
        if (userInfo == null) {
            return null;
        }
        return userInfo.getDefaultAddress();
    }

    public String getCity() {
        if (userInfo == null) {
            return "";
        }
        return userInfo.getCityName();
    }

    public long getBirthday() {
        if (userInfo == null) {
            return 0;
        }
        return userInfo.getBirthday();
    }

    public double getTotalMoney() {
        if (userInfo == null) {
            return 0;
        }
        return userInfo.getTotalMoney();
    }

    public String getSuperviseId(){
        if( userInfo == null){
            return "";
        }
        return userInfo.getSuperviseId();
    }

    public String getSuperviseName() {
        if (userInfo == null) {
            return "";
        }
        return userInfo.getSuperviseName();
    }

    public double getGlebeAceage() {
        if (userInfo == null) {
            return 0;
        }
        return userInfo.getGlebeAceage();
    }

    public double getWetFieldAcreage() {
        if (userInfo == null) {
            return 0;
        }
        return userInfo.getWetFieldAcreage();
    }

    public double getTotalAcreage() {
        if (userInfo == null) {
            return 0;
        }
        return userInfo.getTotalAcreage();
    }
}
